package cc.yaomm.service.basic.mapper.user;

import cc.yaomm.common.basic.base.SuperMapper;
import cc.yaomm.common.basic.domain.Paging;
import cc.yaomm.service.basic.entity.user.Resources;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * Created by XQL on 2021/6/9.
 */
@Mapper
public interface ResourcesMapper extends SuperMapper<Resources> {

    default Page<Resources> findPage(Paging<Resources> paging) {
        LambdaQueryWrapper<Resources> wrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(paging.getCondition())) {
            wrapper.like(Objects.nonNull(paging.getCondition().getAuthLevel()), Resources::getAuthLevel, paging.getCondition().getAuthLevel());
            if (StringUtils.hasText(paging.getCondition().getName()) && StringUtils.hasText(paging.getCondition().getUrl())) {
                wrapper.and(w -> {
                            w.like(Resources::getName, paging.getCondition().getName())
                                    .or()
                                    .like(Resources::getUrl, paging.getCondition().getUrl());
                        }
                );
            } else {
                wrapper.like(StringUtils.hasText(paging.getCondition().getName()), Resources::getName, paging.getCondition().getName());
                wrapper.like(StringUtils.hasText(paging.getCondition().getUrl()), Resources::getUrl, paging.getCondition().getUrl());
            }

        }

        return this.selectPage(paging.toPage(), wrapper);
    }

    /**
     * 根据资源编码查询资源信息
     *
     * @param codes
     * @return
     */
    default List<Resources> findByCodes(Collection<String> codes) {
        if (CollectionUtils.isEmpty(codes)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<Resources> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Resources::getCode, codes);

        return this.selectList(wrapper);
    }

}
